<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible.sync="visible"
    :title="isEdit ? '修改患者回访配置' : '新增患者回访配置'"
    width="800px"
  >
    <el-form :model="form" ref="form" label-width="150px" :rules="rules">
      <el-form-item label="选择部门" prop="deptId">
        <dept-select
          ref="deptSelect"
          :selectValue="form.deptId"
          @select="value => handleTreeSelect('deptId', value)"
        />
      </el-form-item>
      <el-form-item label="最短通话时长（秒）" prop="callDurationStd">
        <el-input v-model.number="form.callDurationStd" placeholder="请输入最短通话时长" />
      </el-form-item>
      <el-form-item label="回访期限（天）" prop="overDays">
        <el-input v-model.number="form.overDays" placeholder="请输入回访期限" />
      </el-form-item>
      <el-form-item label="是否要求医生回访" prop="revisitNurse">
        <el-select clearable v-model="form.revisitDoctor" placeholder="请选择">
          <el-option :value="0" label="否" />
          <el-option :value="1" label="是，非强制" />
          <el-option :value="2" label="强制" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否要求护士回访" prop="revisitNurse">
        <el-select clearable v-model="form.revisitNurse" placeholder="请选择">
          <el-option :value="0" label="否" />
          <el-option :value="1" label="是，非强制" />
          <el-option :value="2" label="强制" />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSure">{{ isEdit ? '修改' : '新增' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import DeptSelect from './DeptSelect'
import { revisitCreate, revisitUpdate, getDeptId } from '@/api/system'
import { openNotification } from '@/utils/util'

export default {
  components: {
    DeptSelect
  },
  data() {
    // const validDept = (rule, value, callback) => {
    //   console.log('validDept', rule, value)
    //   if (!value) {
    //     callback(new Error('请选择部门'))
    //   } else {
    //     if (!this.isEdit) {
    //       console.log('新增============')

    //       if (!this.getDept(value)) {
    //         callback(new Error('部门重复'))
    //       } else {
    //         callback()
    //       }
    //     } else {
    //       if (!this.getDept(value)) {
    //         callback()
    //       } else {
    //         callback()
    //       }
    //     }
    //   }
    // }
    return {
      visible: false,
      isEdit: false,
      form: {
        deptId: null,
        callDurationStd: null,
        overDays: null,
        revisitDoctor: null,
        revisitNurse: null
      },
      selectData: [],
      sureDept: true,
      rules: {
        callDurationStd: [
          { required: true, message: '请输入最短通话时长', trigger: 'blur' },
          { type: 'number', message: '请输入一个数字', trigger: 'blur' },
          { type: 'number', min: 0, message: '最小输入为0', trigger: 'blur' }
        ],
        overDays: [
          { required: true, message: '请输入回访期限', trigger: 'blur' },
          { type: 'number', message: '请输入一个数字', trigger: 'blur' },
          { type: 'number', min: 0, message: '最小输入为0', trigger: 'blur' }
        ],
        deptId: [{ required: true, message: '请选择部门', trigger: 'change' }],
        revisitDoctor: [{ required: true, message: '请选择', trigger: 'blur' }],
        revisitNurse: [{ required: true, message: '请选择', trigger: 'blur' }]
      }
    }
  },
  methods: {
    edit(bool, obj) {
      this.visible = true
      this.isEdit = bool
      this.form = Object.assign({}, obj)
      if (!bool) {
        // 新增设置为0
        this.$nextTick(() => {
          this.$set(this.form, 'deptId', null)
          this.$set(this.form, 'typeOrder', 0)
          this.$refs.deptSelect.reset()
        })
      }
    },
    handleTreeSelect(key, value) {
      console.log('key', key, value)
      this.getDept(value.id)
      this.$set(this.form, key, value.id)
    },
    async getDept(id) {
      const res = await getDeptId(id)
      console.log('res', res)
      this.sureDept = res.data
    },
    handleSure() {
      console.log('sureDept', this.sureDept)

      this.$refs.form.validate(async valid => {
        if (valid && this.sureDept) {
          if (this.form.revisitNurse === 0 && this.form.revisitDoctor === 0) {
            this.$message.error('要求护士回访和要求医生回访不能同时设置为否')
            return false
          } else {
            let res = '',
              mes = ''
            if (this.isEdit) {
              mes = '修改操作成功'
              res = await revisitUpdate(this.form)
            } else {
              mes = '新增操作成功'
              res = await revisitCreate(this.form)
            }
            if (res.state === '000000') {
              openNotification('success', mes)
              this.visible = false
              this.$emit('refresh')
            }
          }
        } else {
          if (!this.sureDept) {
            this.$message.error('该部门已设置回访，不能重复设置')
          }
        }
      })
    }
  }
}
</script>
